package com.siwei.mes.common.query;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.Page;
import com.siwei.mes.common.validator.PageGroup;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author chenmaolin
 */
@Data
public class PageQuery<T, E> {

    @NotNull(message = "当前页为空", groups = {PageGroup.class})
    @Min(value = 1, message = "当前页必须大于等于1", groups = {PageGroup.class})
    private Integer pageNum;

    @NotNull(message = "当前页数量为空", groups = {PageGroup.class})
    @Min(value = 1, message = "当前页数量必须大于等于1", groups = {PageGroup.class})
    private Integer pageSize;

    @Valid
    @NotNull(message = "查询对象为空", groups = {PageGroup.class})
    private T params;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public Page<E> getPage() {
        return new Page(this.pageNum, this.pageSize);
    }

    public Integer getPageNum() {
        return pageNum == null ? 1 : this.pageNum;
    }

    public Integer getPageSize() {
        return pageSize == null ? 10 : this.pageSize;
    }
}
